import ResponseOk from '../utils/ResponseOk';
import { Context, Next } from 'koa';
import { isNil } from 'lodash-es';

//可以记录错误日志信息
export const catchError = async (ctx: Context, next: Next) => {
  try {
    await next();
  } catch (err) {
    if (!isNil(err.errorCode)) {
      ctx.status = err.status || 500;
      ctx.body = ResponseOk.error(err.message, err.code);
    } else {
      ctx.app.emit('error', err, ctx);
    }
  }
};
